// pages/Purchasing/firstphase/firstphase.js
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    firstphaseText: '',//首期
    firstphaserate: '',
    firstphaseyear: '',
    mcinmate: '',
    purchasing: '',
    Moyouth: '',
    houseType: '',
    statisticsArray: [],
    showPurchasing: false,
    showyouth:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.calculate_firstphase();

  },



  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  calculate_firstphase: function () {
    var _showPurchasing = false;
    var _showyouth = false;
    var kfirstphaseText = parseFloat(wx.getStorageSync('kfirstphaseText')) * 10000//首期
    var kfirstphaserate = parseFloat(wx.getStorageSync('kfirstphaserate'))//年利率
    var kfirstphaseyear = parseFloat(wx.getStorageSync('kfirstphaseyear'))//還款年期
    var kMCinmate = wx.getStorageSync('kfirstMCinmate')
    var kPurchasing = wx.getStorageSync('kfirstPurchasing')
    var Moyouth = wx.getStorageSync('kfirstMoyouth');//是否青年职业
    var calculateType = wx.getStorageSync('kfirstcalculateType');//是否楼花
    var _mcinmate = ''; var _purchasing = ''; var _firstMoyouth = '否'; var _calculateType = '否'
    if (kMCinmate == 'MC1') {
      _mcinmate = '是'
      if (Moyouth == 'youth')
        _firstMoyouth = '是'
    }
    if (kMCinmate == 'MC2') {
      _mcinmate = '否';
      _showPurchasing = true,
       _showyouth = true,
      _firstMoyouth = '否'
    }

    if (kPurchasing == 'first') {
      _purchasing = '是'
    }
    if (kPurchasing == 'nofirst')
     { _purchasing = '否',
      _showyouth = true
     }

    if (calculateType == 'Real')
      _calculateType = '否'
    if (calculateType == 'Stage')
      _calculateType = '是'
    var monthrate = (kfirstphaserate / 100) / 12  //月利率
    var qs = kfirstphaseyear * 12;
    var resultArray = new Array();
    for (var i = 9; i >=1; i--) {

      var houseprice = kfirstphaseText / (1 - (i / 10)) //樓價
      var pv = houseprice - kfirstphaseText;//按揭(贷款额)
      //console.log(pv )
      //console.log(Math.ceil((pv / 10000).toFixed(0)))
      //console.log(Math.ceil( pv.toFixed(1) ))

      var installment = (pv * monthrate * Math.pow((1 + monthrate), qs)) / (Math.pow(1 + monthrate, qs) - 1);
      var roundinstallment = util.formatCurrency(Math.ceil(installment.toFixed(1))) + '元'
      var roundhouseprice = ''
      if (houseprice >= 10000)
        roundhouseprice = util.formatCurrency(Math.ceil((houseprice / 10000).toFixed(1))) + '萬'
      else
        roundhouseprice = util.formatCurrency(Math.ceil((houseprice).toFixed(1))) + '元'
      var roundpv = ''

      if (pv >= 10000)
        roundpv = Math.ceil((pv / 10000).toFixed(1)) + '萬(' + (i * 10) + '%)'
      else
        roundpv = Math.ceil((pv).toFixed(1)) + '元(' + (i * 10) + '%)'
      var per = util.JudgementHoust(houseprice, kMCinmate, kPurchasing, Moyouth, calculateType) * 10;
      console.log(houseprice)
    //  if (i <= per) {
        var s = { roundhouseprice, roundpv, roundinstallment, houseprice }
        resultArray.push(s)
     // }


    }
    console.log(resultArray)
    //esultArray = resultArray
    /*var per = util.JudgementHoust(resultArray[0].houseprice, kMCinmate, kPurchasing, Moyouth, calculateType) ;//* 10;
    var maxhouseprice = resultArray[0].houseprice * per;//最大贷款楼价

    resultArray.filter(function (x) {
      return x.houseprice>=maxhouseprice;
    });  
    console.log(maxhouseprice)*/
    this.setData({
      mcinmate: _mcinmate,
      purchasing: _purchasing,
      firstphaseText: '$' + util.formatCurrency(kfirstphaseText),//首期
      firstphaserate: kfirstphaserate + '%',
      firstphaseyear: kfirstphaseyear + '年(' + kfirstphaseyear * 12 + '期)',
      statisticsArray: resultArray,
      showPurchasing: _showPurchasing,
      Moyouth: _firstMoyouth,
      houseType: _calculateType,
      showyouth:_showyouth
    })
  }
})